<?php
namespace app\team\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Index extends Common
{
    public function index(){
        $admin_id   = session('t_admin_id');
    
        $admin_id_pid   = session('t_admin_pid');
        if ($admin_id_pid == '0'){
            $nav = Db::name('team_auth_rule')->field('id,pid,title,icon,name')->where("ismenu = 1")->order("id asc")->select();
        }else{
            //获取所有权限的菜单栏
            $Auth= new \AuthTeam();
            $admin_rules = $Auth->getGroups($admin_id);
            $rules = explode(',',$admin_rules[0]['rules']);
            //获取导航栏列表
            $where['id'] = array('in',$rules);
            $where['ismenu'] = 1;
            $nav = Db::name('team_auth_rule')->field('id,pid,title,icon,name')->where($where)->order("id asc")->select();
        }

        foreach($nav as $key => $value){
            if ($value['pid'] == 0){
                $data[$key]['title'] = $value;
                foreach($nav as $key2 => $value2){
                    if ($value2['pid'] == $value['id']){
                        $data[$key]['nav'][$key2] =$value2;
                        $data[$key]['nav'][$key2]['name'] = url($value2['name']);
                    }
                }
            }
        }

        $this->assign('nav',$data);

        $res = Db::name('team')->where('team_id',Session::get('team')['team_id'])->find();
        $this->assign('res',$res);
        return view();
    }

    public function logout(){
        Session::clear();
        if (!Session::has('t_admin_id')){
            return ['code'=>0];
        }else{
            return ['code'=>1];
        }
    }

    public function editPassword(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $saveData['password'] = $hasher->HashPassword($post['data']['password']);
            $saveData['t_admin_id'] = Session::get('t_admin_id');
            $res = Db::name('team_admin')->update($saveData);
            if ($res){
                Session::clear();
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }
}
